/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkUtil/MkThread.h"
#include "MkEvent/MkTimer.h"
#include "MkUtil/MkLog.h"
#include <signal.h>

void OnTimer1()
{
    MkString StrTime;
    NowMkTime.GetLogString(StrTime);
    printf("timer1 %s\n", StrTime.c_str());
}

void OnTimer2()
{
    MkString StrTime;
    NowMkTime.GetLogString(StrTime);
    printf("timer2 %s\n", StrTime.c_str());
}

void OnTimer3()
{
    MkString StrTime;
    NowMkTime.GetLogString(StrTime);
    printf("timer3 %s\n", StrTime.c_str());
}

void OnTimer4()
{
    MkString StrTime;
    NowMkTime.GetLogString(StrTime);
    printf("timer4 %s\n", StrTime.c_str());
}

void OnTimer5()
{
    MkString StrTime;
    NowMkTime.GetLogString(StrTime);
    printf("timer5 %s\n", StrTime.c_str());
}

CMkTimer* g_Timer = nullptr;
int main(int argc, char** argv)
{
    CMkTimer Timer;
    g_Timer = &Timer;
    MkLogChangeLevel(LOG_DEBUG);
    MkString TimerSession1;
    Timer.AddDelayer(OnTimer1, 40, TimerSession1);
    MkString TimerSession2;
    Timer.AddTimer(OnTimer2, 2000, TimerSession2);
    MkString TimerSession3;
    Timer.AddDelayer(OnTimer3, 3000, TimerSession3);
    MkString TimerSession4;
    Timer.AddDelayer(OnTimer4, 4000, TimerSession4);
    MkString TimerSession5;
    Timer.AddDelayer(OnTimer5, 10000, TimerSession5);
    MkString StrTime;
    NowMkTime.GetLogString(StrTime);
    printf("begin %s\n", StrTime.c_str());

    signal(SIGINT, [](int sig) {
        if (SIGINT == sig && g_Timer) {
            g_Timer->LoopBreak();
        }
    });// set exit  signal

    Timer.Dispatch();
    return 0;
}

